package reflect;

import common.Person;

import java.lang.reflect.Method;

/**
 * @author zuolonglong
 * @version 1.0
 * @description
 * @date 2022/9/9
 */
class Mime {
    public void walkAgainstTheWind() {}

    public void sit() {
        System.out.println("Mime to sit");
    }
}

class SmartDog {
    public void speak() {
        System.out.println("Woof!");
    }

    public void sit() {
        System.out.println("Sitting");
    }
}

class CommunicateReflectively {
    public static void perform(Object speaker) {
        Class<?> spkr = speaker.getClass();
        try {
            try {
                Method speak = spkr.getMethod("speak");
                speak.invoke(speaker);
            } catch (NoSuchMethodException e) {
                System.out.println(speaker + " cannot speak");
            }

            try {
                Method sit = spkr.getMethod("sit");
                sit.invoke(speaker);
            } catch (NoSuchMethodException e) {
                System.out.println(speaker + " cannot sit");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


public class LatentReflection {
    public static void main(String[] args) {
        CommunicateReflectively.perform(new SmartDog());
        CommunicateReflectively.perform(new Mime());
        CommunicateReflectively.perform(new Person());
    }
}